package com.factorit.business.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class GenericDaoImpl implements GenericDao{

	private HibernateTemplate hibernateTemplate;

	private SessionFactory sessionFactory;

	public Object get(String className, Long id) {
		Object result = hibernateTemplate.load(className, id);
		return result;
	}

	@SuppressWarnings("unchecked")
	public List getAll(String className) {
		List result = new ArrayList();
		try {
			result = hibernateTemplate.loadAll(Class.forName(className));
		} catch (DataAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public void save(Object record) {
		hibernateTemplate.saveOrUpdate(record);
	}

	public void delete(String className, Long id) {
		Object record = hibernateTemplate.load(className, id);
		hibernateTemplate.delete(record);
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void delete(Object obj) {
		hibernateTemplate.delete(obj);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void refresh(Object object) {
		hibernateTemplate.refresh(object);
	}
}
